package stud.aistar.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import stud.aistar.entity.CodeStatus;
import stud.aistar.entity.Result;
import stud.aistar.entity.Treatment;
import stud.aistar.service.ITreatmentService;
import stud.aistar.util.LayuiUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * *
 *
 * @ClassName :wangyu
 * @Description
 * @Author
 * @Date2019/9/30 20:55
 * @Version V1.0
 */
@Controller
public class TreatmentController {

    @Autowired
    private ITreatmentService treatmentService;

    @RequestMapping("/treatmentList")
    @ResponseBody
    public Object treatmentList(String docId){

        List<Treatment> treatments = treatmentService.findAll(Integer.valueOf(docId));

        Iterator<Treatment> iterator = treatments.iterator();
        while (iterator.hasNext()){
            Treatment treatment = iterator.next();
            if (!treatment.getDocId().equals(Integer.valueOf(docId))){
                iterator.remove();
            }
        }

//        for (Treatment treatment : treatments) {
//            if (docId !=null){
//                if (!treatment.getDocId().equals(docId)){
//                    treatments.remove(treatment);
//                    break;
//                }
//            }
//        }
        Integer count = treatmentService.FindTreatmentCount();

        return new LayuiUtil<Treatment>(0,"OK",count,treatments);
    }

    @RequestMapping("/pageHelper")
    @ResponseBody
    public Object treatmentPage(@RequestParam String docId,
                                @RequestParam Map<String,String> paramMap){

        PageInfo<Treatment> pageInfo = treatmentService.findPage(Integer.valueOf(docId),paramMap);

        Map<String,Object> map = new HashMap<>();

        map.put("data",pageInfo.getList());
        map.put("code",0);
        map.put("msg","请求成功");
        map.put("count",pageInfo.getTotal());
        return map;
    }

    @PostMapping("/updateList")
    @ResponseBody
    public Result update(HttpServletRequest req){
        String id = req.getParameter("id");
        String patName = req.getParameter("patName");
        String patAge = req.getParameter("patAge");
        String patPhone = req.getParameter("patPhone");
        String docName = req.getParameter("docName");
        String conName = req.getParameter("conName");
        String intro = req.getParameter("intro");
        String patSex = req.getParameter("patSex");
        String doctorId = req.getParameter("doctorId");

        Treatment treatment = new Treatment();
        treatment.setPatName(patName);
        treatment.setPatAge(patAge);
        treatment.setPersonCard(patPhone);
        treatment.setDocName(docName);
        treatment.setConName(conName);
        treatment.setIntro(intro);
        treatment.setTreStatus(0);
        treatment.setPatSex(patSex);
        treatment.setDocId(Integer.valueOf(doctorId));
        treatmentService.updById(treatment,Integer.valueOf(id));

        return new Result(CodeStatus.OK,true,"更新成功");
    }

    @PostMapping("/del")
    @ResponseBody
    public Result delOne(HttpServletRequest req){

        String id = req.getParameter("id");

        treatmentService.delById(Integer.valueOf(id));

        return new Result(CodeStatus.OK,true,"删除成功");
    }

    @RequestMapping("/selectList")
    @ResponseBody
    public Object selectList(@RequestParam String docId,
                             @RequestParam String patName,
                             @RequestParam Map<String,String> paramMap){

        PageInfo<Treatment> pageInfo = treatmentService.findPatNamePage(Integer.valueOf(docId),patName,paramMap);

        Map<String,Object> maps = new HashMap<>();

        maps.put("data",pageInfo.getList());
        maps.put("code",0);
        maps.put("msg","请求成功");
        maps.put("count",pageInfo.getTotal());

        return maps;
    }


}
